package org.myself.meeting.service;

import org.myself.meeting.mapper.EmployeeMapper;
import org.myself.meeting.mapper.MeetingMapper;
import org.myself.meeting.mapper.MeetingParticipantsMapper;
import org.myself.meeting.mapper.MeetingRoomMapper;
import org.myself.meeting.model.Meeting;
import org.myself.meeting.model.MeetingParticipants;
import org.myself.meeting.model.SearchMeeting;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class MyMeetingService {
    @Autowired
    MeetingParticipantsMapper meetingParticipantsMapper;

    @Autowired
    MeetingMapper meetingMapper;


    @Autowired
    MeetingRoomMapper meetingRoomMapper;

    @Autowired
    EmployeeMapper employeeMapper;

    public List<SearchMeeting> getMyMeetingsUserful(Integer employeeid,Integer status) {
        //通过meetingparticipants表找到对应的会议
        List<MeetingParticipants> mps = meetingParticipantsMapper.getByEmpId(employeeid);
        List<SearchMeeting> smts = new ArrayList<SearchMeeting>();
        Date now = new Date();
        for (MeetingParticipants mp: mps) {
            Meeting mt = meetingMapper.getMetByIdSTATUS(mp.getMeetingid(),status);
            if(mt==null){
                continue;
            }
            if(mt.getStarttime().after(now)){
                SearchMeeting smt = new SearchMeeting();
                smt.setMeetingid(mt.getMeetingid());
                smt.setMeetingname(mt.getMeetingname());
                smt.setRoomname(meetingRoomMapper.getMrById(mt.getRoomid()).getRoomname());
                smt.setEmployeename(employeeMapper.getEmpByEmpId(mt.getReservationistid()).getEmployeename());
                smt.setNumberofparticipants(mt.getNumberofparticipants());
                smt.setStarttime(mt.getStarttime());
                smt.setEndtime(mt.getEndtime());
                smt.setReservationtime(mt.getReservationtime());
                smt.setCanceledtime(mt.getCanceledtime());
                smt.setDescription(mt.getDescription());
                smt.setStatus(mt.getStatus());
                smts.add(smt);
            }
        }
        return smts;
    }

    public List<SearchMeeting> getMyMeetingsUnUserful(Integer employeeid,Integer status) {
        //通过meetingparticipants表找到对应的会议
        List<MeetingParticipants> mps = meetingParticipantsMapper.getByEmpId(employeeid);
        List<SearchMeeting> smts = new ArrayList<SearchMeeting>();
        Date now = new Date();
        for (MeetingParticipants mp: mps) {
            Meeting mt = meetingMapper.getMetByIdSTATUS(mp.getMeetingid(),status);
            if(mt==null){
                continue;
            }
            if(mt.getStarttime().before(now)){
                SearchMeeting smt = new SearchMeeting();
                smt.setMeetingid(mt.getMeetingid());
                smt.setMeetingname(mt.getMeetingname());
                smt.setRoomname(meetingRoomMapper.getMrById(mt.getRoomid()).getRoomname());
                smt.setEmployeename(employeeMapper.getEmpByEmpId(mt.getReservationistid()).getEmployeename());
                smt.setNumberofparticipants(mt.getNumberofparticipants());
                smt.setStarttime(mt.getStarttime());
                smt.setEndtime(mt.getEndtime());
                smt.setReservationtime(mt.getReservationtime());
                smt.setCanceledtime(mt.getCanceledtime());
                smt.setDescription(mt.getDescription());
                smt.setStatus(mt.getStatus());
                smts.add(smt);
            }
        }
        return smts;
    }

    public List<SearchMeeting> getMyMeetingsALL(Integer employeeid,Integer status) {
        //通过meetingparticipants表找到对应的会议
        List<MeetingParticipants> mps = meetingParticipantsMapper.getByEmpId(employeeid);
        List<SearchMeeting> smts = new ArrayList<SearchMeeting>();
        for (MeetingParticipants mp: mps) {
            Meeting mt = meetingMapper.getMetByIdSTATUS(mp.getMeetingid(),status);
            if(mt==null){
                continue;
            }
            SearchMeeting smt = new SearchMeeting();
            smt.setMeetingid(mt.getMeetingid());
            smt.setMeetingname(mt.getMeetingname());
            smt.setRoomname(meetingRoomMapper.getMrById(mt.getRoomid()).getRoomname());
            smt.setEmployeename(employeeMapper.getEmpByEmpId(mt.getReservationistid()).getEmployeename());
            smt.setNumberofparticipants(mt.getNumberofparticipants());
            smt.setStarttime(mt.getStarttime());
            smt.setEndtime(mt.getEndtime());
            smt.setReservationtime(mt.getReservationtime());
            smt.setCanceledtime(mt.getCanceledtime());
            smt.setDescription(mt.getDescription());
            smt.setStatus(mt.getStatus());
            smts.add(smt);

        }
        return smts;
    }

    public List<SearchMeeting> getMyMetByme(Integer employeeid,Integer status) {
        List<Meeting> mts = meetingMapper.getMyMetByme(employeeid,status);
        List<SearchMeeting> smts = new ArrayList<SearchMeeting>();
        for (Meeting mt:mts) {
            SearchMeeting smt = new SearchMeeting();
            smt.setMeetingid(mt.getMeetingid());
            smt.setMeetingname(mt.getMeetingname());
            smt.setRoomname(meetingRoomMapper.getMrById(mt.getRoomid()).getRoomname());
            smt.setEmployeename(employeeMapper.getEmpByEmpId(mt.getReservationistid()).getEmployeename());
            smt.setNumberofparticipants(mt.getNumberofparticipants());
            smt.setStarttime(mt.getStarttime());
            smt.setEndtime(mt.getEndtime());
            smt.setReservationtime(mt.getReservationtime());
            smt.setCanceledtime(mt.getCanceledtime());
            smt.setDescription(mt.getDescription());
            smt.setStatus(mt.getStatus());
            smts.add(smt);
        }
        return smts;
    }

}
